<!--
 * @Author: 孙开源 && sunkaiyuan@namenode.cn
 * @Date: 2023-07-24 13:59:42
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-10-31 15:14:48
 * @Description: 终审
 * 
-->
<template>
  <el-dialog  draggable overflow :title="'终审'" v-model="visible" :width="1124" destroy-on-close @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="50px" label-position="right">
      <el-row>
        <el-col :span="5">
          <el-form-item label="客户">
            <el-input v-model="form.company.name" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="品种">
            <el-input v-model="form.material.name" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="批号">
            <el-input v-model="form.lot.name" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="色号">
            <el-input v-model="form.main.code" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="色名">
            <el-input v-model="form.main.name" readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <el-col :span="12">
        配方
        <sc-VxeTable ref="rev" :tablecode="'pro_color_rev_edit_list'" :data="list" :gridOps="gridOpts">
        </sc-VxeTable>
      </el-col>
      <el-col :span="12">
        记录
        <sc-VxeTable ref="history" :tablecode="'pro_color_rev_list_history'" :data="historylist" :gridOps="gridOpts">
        </sc-VxeTable>
      </el-col>
    </el-row>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
export default {

  emits: ["success", "closed"],
  components: {},
  data() {
    return {
      form: {
        main: {},
        material: {},
        company: {},
        lot: {},
        items: []
      },
      rules: {},
      visible: false,
      isSaveing: false,
      list: [],
      historylist: [],
      gridOpts: {
        height: 500,
        columns: [
          {
            field: "newrate",
            event: {
              keyup: ({ row }) => {
                row['newcost'] = row.material?.average_price * row['newrate'] * (row['ounit_'] == 2 ? 1 : 10);
                row['cost'] = row['newcost'] - row['oldcost']
              },
            }
          }
        ],
      },
    };
  },
  mounted() {

  },
  methods: {
    //显示
    open() {
      this.visible = true;
      return this;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.pro.color.faudit.post({ id: this.form.id, list: this.list.filter(res => res.oldrate != res.newrate) });
          this.isSaveing = false;
          if (res.code == 200) {
            this.visible = false;
            this.$emit("success", this.form, this.mode);
            this.$message.success("保存成功");
          }
        }
      });
    },
    getHistory(rev_id) {
      this.$API.pro.color.fauditlist.post({ id: rev_id }).then((res) => {
        this.historylist = res.data;
      });
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, this.$TOOL.objCopy(this.$TOOL.removeNull(data)));
      this.getHistory(data.id);
      this.list = this.form.items.map((item) => {
        item['newrate'] = item['oldrate'] = item['rate'];
        item['newcost'] = item['oldcost'] = item['cost'];
        item['main_id'] = data.id;
        item['color_item_id'] = item.id;
        item.id = 'key' + item.id
        return item;
      });
    },
  },
};
</script>

<style></style>
